<template>
	<div>
		<use-table ref="tbl"></use-table>
		<div ref="advanced" class="container padding_b_0">
			<div class="dflex_wrap">
				<div class="dflex_vertical_c margin_r_40 margin_b_20">
					<div class="search_name">用户名称：</div>
					<el-input v-model="req.member_name" placeholder="请输入" class="search_input" @change="search"
						clearable></el-input>
				</div>
				<div class="dflex_vertical_c margin_r_40 margin_b_20">
					<div class="search_name">手机号：</div>
					<el-input v-model="req.member_mobile" placeholder="请输入" class="search_input" @change="search"
						clearable></el-input>
				</div>
				<div class="dflex_vertical_c margin_r_40 margin_b_20" v-if="advancedSearch">
					<div class="search_name">城市：</div>
					<el-input v-model="req.member_city" placeholder="请输入" class="search_input" @change="search"
						clearable></el-input>
				</div>
				<div class="dflex_vertical_c margin_r_40 margin_b_20" v-if="advancedSearch">
					<div class="search_name">性别：</div>
					<el-select v-model="req.member_gender" class="search_input" @change="search" clearable>
						<el-option v-for="item in genderOptions" :key="item.value" :label="item.label"
							:value="item.value"></el-option>
					</el-select>
				</div>
				<div class="dflex_vertical_c margin_r_40 margin_b_20" v-if="advancedSearch">
					<div class="search_name">注册时间：</div>
					<el-date-picker v-model="searchTime" type="daterange" align="right" range-separator="至"
						start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd"
						:picker-options="pickerTimeOptions" class="search_start_end_time" @change="search">
					</el-date-picker>
				</div>
				<el-button size="mini" :loading="searchBtnLoading" class="search-btn margin_b_20 margin_r_40"
					@click="search">搜索</el-button>
				<div class="search_advanced margin_b_20" @click="advancedFilter" v-if="!advancedSearch">高级筛选</div>
				<div class="search_common margin_b_20" @click="advancedFilter" v-if="advancedSearch">常用筛选</div>
			</div>
		</div>
		<div class="container last_container use-table">
			<el-table ref="etbl" :height="tblHeight" v-loading="tableLoading" :data="tableDatas" highlight-current-row
				@sort-change="sortable">
				<el-table-column label="用户" align="center" width="190">
					<template slot-scope="scope">
						<div class="dflex_vertical_c">
							<el-image class="headimg_small"
								:src="scope.row.member_headimg || 'https://7463-tcb-rflrz31u8yan4rr9ad60d-268679-1308293149.tcb.qcloud.la/1659579992133_0.png'"
								:preview-src-list="scope.row.previewImgs" fit="contain"></el-image>
							<div class="margin_l_5 ellipsis_2 left_just">{{ scope.row.member_name || '--' }}</div>
						</div>
					</template>
				</el-table-column>
				<el-table-column property="is_plus" label="是否会员" align="center"></el-table-column>
				<el-table-column property="member_gender" label="性别" align="center"></el-table-column>
				<el-table-column property="member_login_cnt" label="登录次数" width="100" align="center"></el-table-column>
				<el-table-column label="手机号" align="center">
					<template slot-scope="scope">
						<div>{{ scope.row.member_mobile || '--' }}</div>
					</template>
				</el-table-column>
				<el-table-column label="城市" align="center">
					<template slot-scope="scope">
						<div>{{ scope.row.member_city || '--' }}</div>
					</template>
				</el-table-column>
				<el-table-column label="累计消费" align="center">
					<template slot-scope="scope">
						<div class="price">{{ scope.row.member_monetary / 100 }}</div>
					</template>
				</el-table-column>
				<el-table-column property="create_time" sortable="custom" label="注册时间" align="center" width="180">
				</el-table-column>
				<el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-tooltip :open-delay="350" content="详情" placement="top" :hide-after="1000" :enterable="false"
							effect="light">
							<el-button circle size="medium" icon="el-icon-more-outline" class="table-btn"
								@click="toDetail(scope.row._id)"></el-button>
						</el-tooltip>
					</template>
				</el-table-column>
				<div slot="empty">
					<el-empty description=" "></el-empty>
				</div>
			</el-table>
			<el-pagination background :current-page="req.page" :page-sizes="[10, 20, 30, 50, 100]" :page-size="req.rows"
				layout="total, sizes, prev, pager, next, jumper" :total="tableTotal" @size-change="sizeChange"
				@current-change="currentChange"></el-pagination>
		</div>
	</div>
</template>

<script>
	const __name = 'wmzmall-member';
	export default {
		data() {
			return {
				diff: 0,
				advancedHeight: 0,
				advancedSearch: false,
				searchTime: [],
				genderOptions: [{
						value: '',
						label: '全部'
					},
					{
						value: 1,
						label: '男'
					},
					{
						value: 2,
						label: '女'
					},
					{
						value: 0,
						label: '未知'
					}
				],
				req: {
					page: 1,
					rows: 10,
					orderby: 'create_time desc',
					member_name: '',
					member_mobile: '',
					member_city: '',
					member_gender: '',
					begin_time: '',
					end_time: ''
				},
				tblHeight: 0,
				tableLoading: false,
				tableDatas: [],
				tableTotal: 0,
				pickerTimeOptions: {
					disabledDate(time) {
						return time.getTime() > Date.now();
					},
					shortcuts: [{
							text: '最近一周',
							onClick(picker) {
								const end = new Date();
								const start = new Date();
								start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
								picker.$emit('pick', [start, end]);
							}
						},
						{
							text: '最近一个月',
							onClick(picker) {
								const end = new Date();
								const start = new Date();
								start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
								picker.$emit('pick', [start, end]);
							}
						},
						{
							text: '最近三个月',
							onClick(picker) {
								const end = new Date();
								const start = new Date();
								start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
								picker.$emit('pick', [start, end]);
							}
						}
					]
				},
				searchBtnLoading: false
			};
		},
		methods: {
			async loadData(callback) {
				this.tableLoading = true;

				this.req.begin_time = '';
				this.req.end_time = '';

				if (this.searchTime && this.searchTime.length > 0) {
					this.req.begin_time = this.searchTime[0].getTime();
					this.req.end_time = this.searchTime[1].getTime() + (60 * 60 * 24 - 1) * 1000;
				}

				await this.$db[__name]
					.whereif(this.req.member_name, {
						member_name: new RegExp(this.$api.filterRegexpKey(this.req.member_name), 'i')
					})
					.whereif(this.req.member_mobile, {
						member_mobile: new RegExp(this.$api.filterRegexpKey(this.req.member_mobile), 'i')
					})
					.whereif(this.req.member_city, {
						member_city: new RegExp(this.$api.filterRegexpKey(this.req.member_city), 'i')
					})
					.whereif(this.req.member_gender || this.req.member_gender === 0, {
						member_gender: this.req.member_gender
					})
					.whereif(this.searchTime && this.searchTime.length > 0, {
						create_time: this.$db.$cmd.gte(this.req.begin_time).and(this.$db.$cmd.lte(this.req
							.end_time))
					})
					.totable(this.req)
					.then(res => {
						if (res.code == 200) {
							res.datas.rows.forEach((row, idx) => {
								row.previewImgs = [(row.member_headimg && row.member_headimg.replace(
										"/132", "/0")) ||
									'https://7463-tcb-rflrz31u8yan4rr9ad60d-268679-1308293149.tcb.qcloud.la/1659579992133_0.png'
								];
								row.create_time = new Date(row.create_time).format();
								switch (row.member_gender) {
									case 1:
										row.member_gender = '男';
										break;
									case 2:
										row.member_gender = '女';
										break;
									default:
										row.member_gender = '未知';
										break;
								}
								switch (row.is_plus) {
									case '会员':
										row.is_plus = '否';
										break;
									case '付费会员':
										row.is_plus = '是';
										break;
								}
							});

							this.tableDatas = res.datas.rows;
							this.tableTotal = res.datas.total;

							if (typeof callback === 'function') {
								callback();
							}
						}
						this.searchBtnLoading = false;
						this.tableLoading = false;
					}).catch(() => {
						this.searchBtnLoading = false;
						this.tableLoading = false;
					})
			},
			search() {
				this.req.page = 1;
				this.$api.storagePaginationPage(this.req.page, this.$route.path);
				this.searchBtnLoading = true;

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});

				uni.setStorage({
					key: '__search' + this.$route.path,
					data: this.req
				});
			},
			sortable(column) {
				if (column.order) {
					column.order = column.order.replace('ending', '');
				}
				this.req.orderby = column.prop + ' ' + (column.order || 'desc');

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});
			},
			toDetail(id) {
				uni.navigateTo({
					url: `/pages/user/list/detail?id=${id}&tab=用户详情`,
					events: {
						refreshData: () => {
							this.loadData();
						}
					}
				});
			},
			advancedFilter() {
				this.advancedSearch = !this.advancedSearch;

				this.$nextTick(() => {
					if (this.advancedSearch) {
						this.diff = this.$refs.advanced.clientHeight - this.advancedHeight;
					}
					this.$refs.tbl.tableInit(this.advancedSearch ? -this.diff : this.diff);
					this.$refs.tbl.tableChange();
				});
			},
			sizeChange(size) {
				this.$api.storagePaginationRows(size, this.$route.path);
				this.req.page = 1;
				this.$api.storagePaginationPage(this.req.page, this.$route.path);
				this.req.rows = size;

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});
			},
			currentChange(current) {
				this.$api.storagePaginationPage(current, this.$route.path);
				this.req.page = current;

				this.loadData(() => {
					this.$refs.etbl.bodyWrapper.scrollTop = 0;
				});
			}
		},
		created() {
			let __search = uni.getStorageSync('__search' + this.$route.path);
			if (__search) {
				this.req = __search;
				if (this.req.member_city || this.req.member_gender !== '') {
					this.advancedSearch = true;
				}
				if (this.req.begin_time && this.req.end_time) {
					this.searchTime.push(new Date(this.req.begin_time), new Date(this.req.end_time));
					this.advancedSearch = true;
				}
			}

			this.req.page = uni.getStorageSync('__paginationPage' + this.$route.path) || 1;
			this.req.rows = uni.getStorageSync('__paginationRows' + this.$route.path) || 10;

			this.loadData(() => {
				this.$nextTick(() => {
					this.$api.scrollToStorage('.el-table__body-wrapper', this.$route.path);
				});
			});
		},
		mounted() {
			this.advancedHeight = this.$refs.advanced.clientHeight;

			this.$nextTick(() => {
				this.$api.scrollOnStorage('.el-table__body-wrapper', this.$route.path);
			});
		},
		updated() {
			if (!this.tblHeight) {
				this.tblHeight = this.$refs.tbl.tblHeight;
			}
		}
	};
</script>

<style>
	.ellipsis_2 {
		max-width: 100px;
	}
</style>
